from typing import List

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.gridspec import GridSpec

from . import config


class Logging:
    
    def __init__(self, num_of_subchart: int) -> None:
        """初始化测井曲线图绘制工具

        Args:
            num_of_subchart (int): 绘制图中的子图数量
        """
        
        config.set_font_size(15)
        config.set_figure_size([2 * num_of_subchart, 9])
        config.set_tick_direction('y', config.TickDirection.IN)
        config.set_grid_axis(config.GridAxis.Y)
        config.enable_grid(True)
        config.enable_tick('x', config.TickItemLoction.BOTTOM, False)
        config.enable_tick_label('x', config.TickItemLoction.BOTTOM, False)
        config.enable_figure_constrained_layout(True)
        config.set_figure_pad(config.FigurePadDirection.VIRTICAL, 0)
        config.set_figure_pad(config.FigurePadDirection.HORIZONTAL, 0)
        config.set_font_family(config.FontFamily.TIMES_NEW_ROMAN)
        self.__fig = plt.figure()
        self.__gs=GridSpec(8, num_of_subchart)
        plt.subplots_adjust(wspace=0)
        plt.subplots_adjust(hspace=0)
        
    
    def draw(self,
             x_data: List[np.ndarray],
             y_data: np.ndarray,
             chart_order: int,
             labels: List[str],
             line_style_list: List[str],
             color_list: List[str],
             line_width_list: List[float],
             y_reverse: bool = True) -> None:
        """测井曲线图绘制

        Args:
            x_data (List[np.ndarray]): 子图x轴数据（不同曲线）
            y_data (np.ndarray): 子图y轴数据（深度）
            chart_order (int): 子图序号
            labels (List[str]): 曲线名称列表
            line_style_list (List[str]): 曲线绘制线型列表
            color_list (List[str]): 曲线绘制颜色列表
            line_width_list (List[float]): 曲线绘制线宽列表
            y_reverse (bool, optional): 深度是否进行反转，默认深度由浅到深.
        """
        
        flag = True if chart_order == 0 else False
        config.enable_tick('y', config.TickItemLoction.LEFT, flag)
        config.enable_tick_label('y', config.TickItemLoction.LEFT, flag)
        config.enable_grid(True)
        self.__fig.add_subplot(self.__gs[1:8, chart_order])
        
        x_min_range = np.min(x_data) - 0.05
        x_max_range = np.max(x_data) + 0.05
        y_min_range = np.min(y_data)
        y_max_range = np.max(y_data)
        if y_reverse:
            y_min_range, y_max_range = y_max_range, y_min_range
        
        plt.xlim(x_min_range, x_max_range)
        plt.ylim(y_min_range, y_max_range)
        
        for i in range(len(x_data)):
            plt.plot(x_data[i], y_data, linestyle=line_style_list[i], color=color_list[i], linewidth=line_width_list[i])
        
        config.enable_tick('y', config.TickItemLoction.LEFT, False)
        config.enable_tick_label('y', config.TickItemLoction.LEFT, False)
        config.enable_grid(False)
        self.__fig.add_subplot(self.__gs[0, chart_order])
        
        plt.xlim(-1, 1)
        plt.ylim(0, len(x_data) + 1)
        x = np.linspace(-1, 1, 100)
        for i in range(len(x_data)):
            y = [i + 1] * 100
            plt.plot(x, y, linestyle=line_style_list[i], color=color_list[i], linewidth=line_width_list[i])
            plt.text(0, i+1.45, labels[i], ha="center", va="center", fontdict={"weight": "bold"})
            plt.text(-0.95, 0.1, round(x_min_range+0.05, 1), ha="left")
            plt.text(0.95, 0.1, round(x_max_range-0.05, 1), ha="right")